package com.ruoyi.comment.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.comment.domain.CommerceInfoComment;
import com.ruoyi.comment.service.ICommerceInfoCommentService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comment.mapper.SystCommentMapper;
import com.ruoyi.comment.domain.SystComment;
import com.ruoyi.comment.service.ISystCommentService;

/**
 * 评论信息Service业务层处理
 * 
 * @author zhezhi
 * @date 2022-10-02
 */
@Service
public class SystCommentServiceImpl implements ISystCommentService 
{
    @Autowired
    private SystCommentMapper systCommentMapper;

    @Autowired
    private ICommerceInfoCommentService InfoCommentService;

    /**
     * 查询评论信息
     * 
     * @param commentId 评论信息主键
     * @return 评论信息
     */
    @Override
    public SystComment selectSystCommentByCommentId(Long commentId)
    {
        SystComment systComment = systCommentMapper.selectSystCommentByCommentId(commentId);
        CommerceInfoComment commerceInfoComment = InfoCommentService.selectCommerceInfoCommentByCommentId(commentId);
        systComment.setCommerceId(commerceInfoComment.getCommerceId());
        return systComment;
    }

    /**
     * 查询评论信息列表
     * 
     * @param systComment 评论信息
     * @return 评论信息
     */
    @Override
    public List<SystComment> selectSystCommentList(SystComment systComment)
    {
        List<SystComment> systComments = systCommentMapper.selectSystCommentList(systComment);
//        new ArrayList<>()
        for(SystComment sys : systComments){
            CommerceInfoComment commerceInfoComment = InfoCommentService.selectCommerceInfoCommentByCommentId(sys.getCommentId());
            if(commerceInfoComment.getCommerceId() == null){
                sys.setCommerceId(0L);
            }else {
                sys.setCommerceId(commerceInfoComment.getCommerceId());
            }

        }

        return systComments;
    }

    /**
     * 新增评论信息
     * 
     * @param systComment 评论信息
     * @return 结果
     */
    @Override
    public int insertSystComment(SystComment systComment)
    {
        systComment.setCreateTime(DateUtils.getNowDate());
        int i = systCommentMapper.insertSystComment(systComment);
        SystComment systComment1 = systCommentMapper.selectSystCommentByEnd();
        CommerceInfoComment infoComment = new CommerceInfoComment();
        infoComment.setCommentId(systComment1.getCommentId());
        infoComment.setCommerceId(systComment.getCommerceId());
        int i1 = InfoCommentService.insertCommerceInfoComment(infoComment);
        return i;
    }

    /**
     * 修改评论信息
     * 
     * @param systComment 评论信息
     * @return 结果
     */
    @Override
    public int updateSystComment(SystComment systComment)
    {
        systComment.setUpdateTime(DateUtils.getNowDate());

        if(systComment.getCommerceId()!= InfoCommentService.selectCommerceInfoCommentByCommentId(systComment.getCommentId()).getCommerceId()){
            CommerceInfoComment commerceInfoComment = new CommerceInfoComment();
            commerceInfoComment.setCommerceId(systComment.getCommerceId());
            commerceInfoComment.setCommentId(systComment.getCommentId());
            int i = InfoCommentService.updateCommerceInfoCommentByCommentId(commerceInfoComment);
        }

        return systCommentMapper.updateSystComment(systComment);
    }

    /**
     * 批量删除评论信息
     * 
     * @param commentIds 需要删除的评论信息主键
     * @return 结果
     */
    @Override
    public int deleteSystCommentByCommentIds(Long[] commentIds)
    {
        /**
         * 如果要进行校验操作可以设置total , 上来获取Long的长度，然后里面每次i+最后比较两者的大小
         */
        for(Long id: commentIds){
            CommerceInfoComment commerceInfoComment = InfoCommentService.selectCommerceInfoCommentByCommentId(id);
            int i = InfoCommentService.deleteCommerceInfoCommentByCommerceId(commerceInfoComment.getCommerceId());
        }

        return systCommentMapper.deleteSystCommentByCommentIds(commentIds);
    }

    /**
     * 删除评论信息信息
     * 
     * @param commentId 评论信息主键
     * @return 结果
     */
    @Override
    public int deleteSystCommentByCommentId(Long commentId)
    {
        CommerceInfoComment commerceInfoComment = InfoCommentService.selectCommerceInfoCommentByCommentId(commentId);
        int i = InfoCommentService.deleteCommerceInfoCommentByCommerceId(commerceInfoComment.getCommerceId());
        return systCommentMapper.deleteSystCommentByCommentId(commentId);
    }
}
